<?php

	class SearchController extends AppController
	{
		var $uses = array('Product');
		var $publicAction = array('index');
		
		function index($q = null)
		{
			App::import('Core', 'Sanitize');
			
			$this->pageTitle = __('Buscar', true);
			if ($q != null) {
				$this->pageTitle = sprintf('%s: %s', $this->pageTitle, $q);
			}
			
			if (!empty($this->data)) {
				$q = Sanitize::escape($this->data['Search']['q']);
				$q = urlencode($q);
				$this->redirect(array('controller' => 'search', 'action' => 'index', $q));
			}
			
			$q = explode(' ', $q);
			$q_ = array();
			foreach($q AS $r) {
				$q_[] = '+' . $r;
			}
			$q = $q_;
			
			$conditions = 'MATCH(ProductDescription.description) AGAINST(\'' . implode(' ', $q) . '\')'
			//. ' AND Product.disabled = 0'
			//. ' AND Product.deleted = 0'
			;
			$this->Product->ProductDescription->Behaviors->attach('Containable');
			$products = $this->Product->ProductDescription->find(
				'all',
				array(
					'conditions' => $conditions,
					'order' => 'MATCH(ProductDescription.description) AGAINST(\'' . implode(' ', $q) . '\') DESC',
					'contain' => array(
						'Product'
					),
					'recursive' => -1
				)
			);
			
		}
	}

?>